﻿#region <!-- Using Directives -->

using System;

using Office = Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;

#endregion

namespace WordSharpDev.Extensions
{
	internal static class WordExtensions
	{
		#region <!-- PasteFrom -->

		public static void PasteFrom(this Word.Range p_Target, Word.Range p_Source)
		{
			p_Source.Copy();
			p_Target.Paste();

			var l_LastChar = p_Target;

			l_LastChar.SetRange(p_Target.End, p_Target.End);
			l_LastChar.Delete(Count: 1);

			Object l_Style = p_Target.get_Style();

			if (l_Style != null)
			{
				((Word.Style)l_Style).ParagraphFormat.Space1();
				((Word.Style)l_Style).ParagraphFormat.SpaceAfter = 0.0f;
				((Word.Style)l_Style).ParagraphFormat.SpaceBefore = 0.0f;
			}
		}

		#endregion

		#region <!-- GetBuiltInPropertyString -->

		public static String GetBuiltInPropertyString(this Word.Document p_This, Word.WdBuiltInProperty p_Name)
		{
			var l_Result = default(String);

			try
			{
				var l_Property = ((Office.DocumentProperties)p_This.BuiltInDocumentProperties)[p_Name];

				if (l_Property.Type == Office.MsoDocProperties.msoPropertyTypeString) l_Result = Convert.ToString(l_Property.Value);
			}
			catch
			{
			}

			return l_Result;
		}

		public static String GetBuiltInPropertyString(this Microsoft.Office.Tools.Word.DocumentBase p_This, Word.WdBuiltInProperty p_Name)
		{
			var l_Result = default(String);

			try
			{
				var l_Property = ((Office.DocumentProperties)p_This.BuiltInDocumentProperties)[p_Name];

				if (l_Property.Type == Office.MsoDocProperties.msoPropertyTypeString) l_Result = Convert.ToString(l_Property.Value);
			}
			catch
			{
			}

			return l_Result;
		}

		#endregion

		#region <!-- SetBuiltInPropertyString -->

		public static void SetBuiltInPropertyString(this Word.Document p_This, Word.WdBuiltInProperty p_Name, String p_Value)
		{
			try
			{
				((Office.DocumentProperties)p_This.BuiltInDocumentProperties)[p_Name].Value = p_Value;
			}
			catch
			{
			}
		}

		public static void SetBuiltInPropertyString(this Microsoft.Office.Tools.Word.DocumentBase p_This, Word.WdBuiltInProperty p_Name, String p_Value)
		{
			try
			{
				((Office.DocumentProperties)p_This.BuiltInDocumentProperties)[p_Name].Value = p_Value;
			}
			catch
			{
			}
		}

		#endregion

		#region <!-- GetCustomPropertyString -->

		public static String GetCustomPropertyString(this Word.Document p_This, String p_Name)
		{
			var l_Result = default(String);

			try
			{
				var l_Property = ((Office.DocumentProperties)p_This.CustomDocumentProperties)[p_Name];

				if (l_Property.Type == Office.MsoDocProperties.msoPropertyTypeString) l_Result = Convert.ToString(l_Property.Value);
			}
			catch
			{
			}

			return l_Result;
		}

		public static String GetCustomPropertyString(this Microsoft.Office.Tools.Word.DocumentBase p_This, String p_Name)
		{
			var l_Result = default(String);

			try
			{
				var l_Property = ((Office.DocumentProperties)p_This.CustomDocumentProperties)[p_Name];

				if (l_Property.Type == Office.MsoDocProperties.msoPropertyTypeString) l_Result = Convert.ToString(l_Property.Value);
			}
			catch
			{
			}

			return l_Result;
		}

		#endregion

		#region <!-- SetCustomPropertyString -->

		public static void SetCustomPropertyString(this Word.Document p_This, String p_Name, String p_Value)
		{
			try
			{
				((Office.DocumentProperties)p_This.CustomDocumentProperties)[p_Name].Value = p_Value;
			}
			catch
			{
				((Office.DocumentProperties)p_This.CustomDocumentProperties).Add(p_Name, false, Type: Office.MsoDocProperties.msoPropertyTypeString, Value: p_Value);
			}
		}

		public static void SetCustomPropertyString(this Microsoft.Office.Tools.Word.DocumentBase p_This, String p_Name, String p_Value)
		{
			try
			{
				((Office.DocumentProperties)p_This.CustomDocumentProperties)[p_Name].Value = p_Value;
			}
			catch
			{
				((Office.DocumentProperties)p_This.CustomDocumentProperties).Add(p_Name, false, Type: Office.MsoDocProperties.msoPropertyTypeString, Value: p_Value);
			}
		}

		#endregion

		#region <!-- GetCharacter -->

		public static String GetCharacter(this Word.Table p_This, Int32 p_Index)
		{
			if (p_This == null) return null;

			p_Index = p_Index % p_This.Range.Characters.Count;

			return p_This.Range.Characters[p_Index + 1].Text;
		}

		#endregion

		#region <!-- GetString -->

		public static String GetString(this Word.Table p_This, Int32 p_FromIndex)
		{
			if (p_This == null) return null;

			p_FromIndex = p_FromIndex % p_This.Range.Characters.Count;

			return String.Format("{0}", p_This.Range.Text.Substring(p_FromIndex));
		}

		public static String GetString(this Word.Table p_This, Int32 p_FromIndex, Int32 p_ToIndex)
		{
			if (p_This == null) return null;

			p_FromIndex = p_FromIndex % p_This.Range.Characters.Count;
			p_ToIndex = p_ToIndex % p_This.Range.Characters.Count;

			if (p_FromIndex <= p_ToIndex)
			{
				return String.Format("{0}", p_This.Range.Text.Substring(p_FromIndex, p_ToIndex - p_FromIndex + 1));
			}
			else
			{
				return String.Format("{0}{1}", p_This.Range.Text.Substring(p_FromIndex, p_This.Range.Text.Length - p_FromIndex), p_This.Range.Text.Substring(0, p_ToIndex + 1));
			}
		}

		#endregion

		#region <!-- GetLength -->

		public static Int32 GetLength(this Word.Table p_This)
		{
			if (p_This == null) return 0;

			return p_This.Range.Characters.Count;
		}

		#endregion
	}
}